package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.ThreadUtil;
import com.weiyi.mashangzhu.easemob.DemoHelper;
import com.weiyi.mashangzhu.easemob.db.ChatDao;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeNameActivity extends BaseActivity {

    @BindView(R.id.change_name)
    EditText changeName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_name);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.back, R.id.save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.save:
                if ((changeName.getText()+"").equals("")){
                    T.showShort(this,"用户名不能为空");
                }else {
                    LoadingDialog.show(this);
                    final String token = SPUtils.get(this, "token", "") + "";
                    final String nick = changeName.getText() + "";
                    OkHttpUtils.get()
                            .addParams("token", token)
                            .addParams("uNickname",nick)
                            .url(API.BaseApi_R+API.ChangeName)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Request request, Exception e) {
                                    LoadingDialog.dismiss(ChangeNameActivity.this);
                                    T.showShort(ChangeNameActivity.this,"数据获取失败，请稍候再试");
                                }

                                @Override
                                public void onResponse(String response) {
                                    LoadingDialog.dismiss(ChangeNameActivity.this);
                                    try {
                                        JSONObject jsonObject=new JSONObject(response);
                                        if (jsonObject.getInt("status")==0){
                                            updateRemoteNick(token, nick);
                                            finish();
                                        }
                                        String msg = jsonObject.getString("msg");
                                        T.showShort(ChangeNameActivity.this, msg);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                }
                break;
        }
    }

    private void updateRemoteNick(String token, String nickName) {
        if (token != null) {
            String[] split = token.split("@");
            if (split.length > 1) {
                String userAccount = split[1];
                if (userAccount != null) {
                    ChatDao chatDao = new ChatDao(this);
                    chatDao.update(userAccount, nickName, "");
                }
            }
        }
    }
}
